#!/usr/bin/env python
import itk
import sys
from optparse import OptionParser
import os
import imageTools.ITKUtils.io as io
import imageTools.ITKUtils.ifilter as ifilter
import numpy as np

def getParser():
    parser = OptionParser()
    parser.add_option("-i","--img",dest='fname',
                      help='name of image to read')
    parser.add_option("-t","--thresh",dest='threshold',nargs=2,default=(0,10000),type='int')
    parser.add_option("-s","--seed",dest='seed',nargs=3,default=(0,0,0),
                      type='int')
    parser.add_option("-m", "--mask", dest="maskfile" , default = 'None' )
    return parser

def run_seg(fname, low_th, high_th):
    rimg = io.readImage(fname, imgMode = 'float')
    filter = itk.ThresholdImageFilter.IF3.New()
    filter.SetInput(rimg)
    filter.SetLower(low_th)
    filter.SetUpper(high_th)
    mask = filter.GetOutput()
    mask.Update()
    # get output name
    tmp = os.path.splitext(fname)
    io.writeImage(mask,tmp[0]+"_pe_region"+tmp[1],outMode="float")
def main():
    fname = sys.argv[1]
    low_th = float(sys.argv[2])
    high_th = float(sys.argv[3])
    print fname
    run_seg(fname, low_th, high_th)  
    
if __name__ == '__main__':
    main()


